/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DisciplinaEdicaoGUI.java
 *
 * Created on 18/06/2011, 19:28:53
 */
package GUI;

import bo.DisciplinaBO;
import entidades.Disciplina;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import logic.FuncoesDisciplinaGUI;

/**
 *
 * @author Ricardo
 */
public class DisciplinaEdicaoGUI extends javax.swing.JFrame {
    int alteracao = 0;
    String nomeDisciplinaALteracao;

    /** Creates new form DisciplinaEdicaoGUI */
    public DisciplinaEdicaoGUI(Disciplina disciplina, int alterar) {
        if(alterar == 1){
            initComponents();
            nomeDisciplinaALteracao = disciplina.getNomeDisciplina();
            jTextFieldNomeDisciplina.setText(disciplina.getNomeDisciplina());
            jTextAreaTopicos.setText(disciplina.getTopico());
            alteracao = 1;
        }
        else
            initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelNomeDisciplina = new javax.swing.JLabel();
        jLabelTopicosDisciplina = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaTopicos = new javax.swing.JTextArea();
        jButtonConfirma = new javax.swing.JButton();
        jButtonLimpar = new javax.swing.JButton();
        jLabelTitulo = new javax.swing.JLabel();
        jTextFieldNomeDisciplina = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelNomeDisciplina.setText("Nome disciplina:");

        jLabelTopicosDisciplina.setText("<html>\nTópicos disciplina:<br /> <br />\nEscreva os tópicos <br />\nseparando-os com <br />\nponto e virgula ( ; )\n</html>");

        jTextAreaTopicos.setColumns(20);
        jTextAreaTopicos.setRows(5);
        jScrollPane2.setViewportView(jTextAreaTopicos);

        jButtonConfirma.setText("Confirma");
        jButtonConfirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmaActionPerformed(evt);
            }
        });

        jButtonLimpar.setText("Limpar");
        jButtonLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimparActionPerformed(evt);
            }
        });

        jLabelTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabelTitulo.setText("Escolha as opções para edição de disciplina");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelTitulo)
                        .addGap(67, 67, 67))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelTopicosDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelNomeDisciplina))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButtonLimpar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 201, Short.MAX_VALUE)
                                .addComponent(jButtonConfirma))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
                            .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTitulo)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelNomeDisciplina)
                    .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelTopicosDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirma)
                    .addComponent(jButtonLimpar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimparActionPerformed
        jTextAreaTopicos.setText(null);
        jTextFieldNomeDisciplina.setText(null);
    }//GEN-LAST:event_jButtonLimparActionPerformed

    private void jButtonConfirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmaActionPerformed
        try{
            Disciplina disciplina = new Disciplina();

            if(!(jTextFieldNomeDisciplina.getText().isEmpty()) && !(jTextAreaTopicos.getText().isEmpty())){
                disciplina.setNomeDisciplina(jTextFieldNomeDisciplina.getText());
                disciplina.setTopico(jTextAreaTopicos.getText());

                if(alteracao == 0){
                    try {
                        DisciplinaBO.getInstance().criar(disciplina);
                    } catch (Exception ex) {
                        Logger.getLogger(DisciplinaEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else {
                    try {
                        DisciplinaBO.getInstance().alterar(FuncoesDisciplinaGUI.ProcurarDisciplina(nomeDisciplinaALteracao), disciplina);
                    } catch (Exception ex) {
                        Logger.getLogger(DisciplinaEdicaoGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                JOptionPane.showMessageDialog(this,"Operação realizada com sucesso!",this.getTitle() ,JOptionPane.INFORMATION_MESSAGE);
                DisciplinaEdicaoGUI.this.dispose();
            } else
                JOptionPane.showMessageDialog(this, "Todos os dados devem ser preenchidos", this.getTitle(), JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            showErrorMessage(e.getMessage());
        }
    }//GEN-LAST:event_jButtonConfirmaActionPerformed

    private void showErrorMessage(String message) {
        JOptionPane.showMessageDialog(this,"Entrada inválida " + message,this.getTitle() ,JOptionPane.ERROR_MESSAGE);
    }

    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DisciplinaEdicaoGUI(null,0).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirma;
    private javax.swing.JButton jButtonLimpar;
    private javax.swing.JLabel jLabelNomeDisciplina;
    private javax.swing.JLabel jLabelTitulo;
    private javax.swing.JLabel jLabelTopicosDisciplina;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaTopicos;
    private javax.swing.JTextField jTextFieldNomeDisciplina;
    // End of variables declaration//GEN-END:variables
}